﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using protoNet;
using VitServer;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SignIn : MonoBehaviour {

    [SerializeField] private GameObject board_guest;
    [SerializeField] private GameObject board_signUp;
    [SerializeField] private Text text_account;
    [SerializeField] private Text text_password;
  
    void Start ()
    {
        MsgReader.OnMessageRecieved(GAME_PROTOCOL_MSG_ID.ID_S2C_LOGIN, OnLoginCallBack);
        MsgReader.OnMessageRecieved(GAME_PROTOCOL_MSG_ID.ID_C2S_LOGIN_LOBBY, OnLobbyCallBack);
    }
    

    void Update ()
    {
		
	}

    public void OnClickSignIn()
    {
        string account = text_account.text;
        string password = text_password.text;
        MsgSender.SendID_C2S_ACCOUNT(account, password);
    }

    public void OnClickGuest()
    {
        MsgSender.SendID_C2S_GUEST();
    }

    public void OnClickSignUp()
    {
        board_signUp.SetActive(true);
        gameObject.SetActive(false);
    }
    //服务器返回登录及注册信息
    private void OnLoginCallBack(object message)
    {
        var mes = (MsgLoginRep)message;
        if (mes != null)
        {
            _G.Print("type:" + mes.type.ToString());
            _G.Print("token:" + mes.token);
            _G.Print("host:" + mes.host);
            _G.Print("openId:" + mes.openId);
            //TODO..保存登录或注册返回的信息
            //根据type确定是那种请求的返回
            //type =0为游客登录，type=2为注册，type=3为账密登录
            //host为大厅服务器IP，连接大厅时需要使用此IP
            //token为当前用户凭据，连接大厅时需要发送
            //openID没用
            _G.Print("LoadingStartText.OnLogin has called");

            //测试能否连接大厅服务器，send方法加了缺省参数之后再改，ip应为当前设备ip
            //string ip = "http://" + "192.168.0.2";           
            if (mes.type == 0 || mes.type == 3)
            {
                SaveHallData.Instance.SetHallData(mes.type, mes.token, mes.host, mes.openId);
                 SceneManager.LoadScene("GameHall");
            }           
            //MsgSender.SendID_C2S_LOGIN_LOBBY(mes.token, ip, mes.host);
        }

    }

    void OnLobbyCallBack(object message)
    {
        _G.Print("LoadingStartText.OnLobbyCallBack has called");
    }
}
